package com.holly.unit.bpmn.activiti.ext;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.holly.unit.bpmn.activiti.entity.ActivitiZNodeAuth;
import com.holly.unit.bpmn.activiti.mapper.ActivitiZNodeAuthMapper;
import com.holly.unit.bpmn.activiti.pojo.ActivitiZNodeAuthRequest;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * 类描述: 流程表单字段控制表业务类
 *
 * @author fxr
 * @version 1.0.0
 * @date 2022/4/20 8:03
 */
@Service
public class ActivitiZNodeAuthService extends ServiceImpl<ActivitiZNodeAuthMapper, ActivitiZNodeAuth> {


    public void saveActZNodeAuth(ActivitiZNodeAuthRequest request) {
        QueryWrapper<ActivitiZNodeAuth> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("process_id", request.getProcessId());
        queryWrapper.eq("task_id", request.getTaskId());
        queryWrapper.eq("form_biz_code", request.getFormBizCode());
        this.baseMapper.delete(queryWrapper);
        List<ActivitiZNodeAuth> list = new ArrayList<>();
        for (Object ite : request.getFormArray()) {
            JSONObject auth = JSONObject.parseObject(JSONObject.toJSONString(ite));
            ActivitiZNodeAuth nodeAuth1 = new ActivitiZNodeAuth();
            nodeAuth1.setProcessId(request.getProcessId());
            nodeAuth1.setTaskId(request.getTaskId());
            nodeAuth1.setFormBizCode(request.getFormBizCode());
            nodeAuth1.setRuleCode(auth.getString("ruleCode"));
            nodeAuth1.setRuleType(auth.getString("ruleType"));
            nodeAuth1.setRuleName(auth.getString("ruleName"));
            nodeAuth1.setDesformComKey(auth.getString("desformComKey"));
            list.add(nodeAuth1);
        };
        this.saveBatch(list);
    }
}
